package com.example.petplatform.controller;

import com.example.petplatform.common.Result;
import com.example.petplatform.dao.DonationRecordDao;
import com.example.petplatform.dto.DonationAddDTO;
import com.example.petplatform.dto.DonationContributeDTO;
import com.example.petplatform.dto.DonationUpdateDTO;
import com.example.petplatform.entity.Donation;
import com.example.petplatform.entity.DonationRecord;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.service.DonationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 捐款管理模块
 */
@RestController
@RequestMapping("donation")
public class DonationController {
    @Autowired
    private DonationService donationService;
    @Resource
    private DonationRecordDao donationRecordDao;
    @GetMapping("/list")
    public Result list(Params params) {
        PageInfo<Donation> list = donationService.donationSearch(params);
        return Result.success(list);
    }

    @GetMapping("/statistics")
    public Result getStatistics() {
        return Result.success(donationService.getStatistics());
    }

    @PostMapping("/add")
    public Result add(@RequestBody DonationAddDTO addDTO) {
        donationService.add(addDTO);
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody DonationUpdateDTO updateDTO) {
        donationService.update(updateDTO);
        return Result.success("更新成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        donationService.delete(id);
        return Result.success("删除成功");
    }

    @PostMapping("/contribute")
    public Result contribute(@RequestBody DonationContributeDTO contributeDTO) {
        donationService.contribute(contributeDTO);
        return Result.success("捐款成功，感谢您的爱心！");
    }

    @GetMapping("/records/project/{projectId}")
    public Result getProjectRecords(@PathVariable Long projectId, Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<DonationRecord> records = donationRecordDao.findByProjectId(projectId, params);
        return Result.success(new PageInfo<>(records));
    }

    @GetMapping("/records/user/{userId}")
    public Result getUserRecords(@PathVariable Long userId, Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<DonationRecord> records = donationRecordDao.findByUserId(userId, params);
        return Result.success(new PageInfo<>(records));
    }
}
